package com.yztc.orangeentertainment.ui.main.subscribefragment.starlist;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.yztc.orangeentertainment.R;
import com.yztc.orangeentertainment.adapter.StarListAdapter;
import com.yztc.orangeentertainment.base.BaseActivity;
import com.yztc.orangeentertainment.base.OnItemClickListener;
import com.yztc.orangeentertainment.bean.starlistbean.ListBean;
import com.yztc.orangeentertainment.config.SubscribeUrlConfig;
import com.yztc.orangeentertainment.ui.main.subscribefragment.staritem.StarItemActivity;
import com.yztc.orangeentertainment.utils.DividerItemDecoration;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by My on 2016/10/7.
 * 明星库
 */
public class StarListActivity extends BaseActivity implements StarListContract.View, View.OnClickListener {
    @InjectView(R.id.starlist_back)
    ImageView starlistBack;
    @InjectView(R.id.et_starlist)
    EditText mEditText;
    @InjectView(R.id.tv_starlist_search)
    TextView tvStarlistSearch;
    @InjectView(R.id.rv_starlist)
    RecyclerView mRecyclerView;
    @InjectView(R.id.swipe_starlist)
    SwipeRefreshLayout mSwipe;
    private StarListPresenter presenter;
    private int page = 1;
    private StarListAdapter adapter ;
    private GridLayoutManager gridLayoutManager;
    private int lastVisibleItem;
    private List<ListBean> totalList = new ArrayList<>();
    private boolean flag;

    @Override
    public int getLayoutId() {
        return R.layout.activity_starlist;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.inject(this);
        initView();
        initPresenter();
    }

    /**
     * 初始化视图
     */
    private void initView() {
        adapter = new StarListAdapter(this);
        starlistBack.setOnClickListener(this);
        tvStarlistSearch.setOnClickListener(this);
        //mRecyclerView管理器
        gridLayoutManager = new GridLayoutManager(this, 4);
        //间距
        mRecyclerView.addItemDecoration(new DividerItemDecoration(20));
        mRecyclerView.setLayoutManager(gridLayoutManager);

        //上拉加载
        mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                lastVisibleItem = gridLayoutManager.findLastVisibleItemPosition();
            }

            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE && (lastVisibleItem + 1) == adapter.getItemCount()) {
                    page++;
                    mSwipe.setRefreshing(true);
                    initPresenter();
                    adapter.notifyDataSetChanged();
                    mSwipe.setRefreshing(false);
                }

            }
        });
    }

    /**
     * 初始化下方数据present 以及传参
     */
    private void initPresenter() {
        presenter = new StarListPresenter(this);
        Map<String, String> map = new HashMap<>();
        map.put(SubscribeUrlConfig.Params.PAGE, page + "");
        map.put(SubscribeUrlConfig.Params.TS, SubscribeUrlConfig.All_DefaultVaule.TS_VALUE);
        map.put(SubscribeUrlConfig.Params.MAC, SubscribeUrlConfig.All_DefaultVaule.MAC_VALUE);
        map.put(SubscribeUrlConfig.Params.UID, SubscribeUrlConfig.All_DefaultVaule.UID_VALUE);
        map.put(SubscribeUrlConfig.Params.PF, SubscribeUrlConfig.All_DefaultVaule.PF_VALUE);
        map.put(SubscribeUrlConfig.Params.NET, SubscribeUrlConfig.All_DefaultVaule.NET_VALUE);
        map.put(SubscribeUrlConfig.Params.ACCESSTOKEN, SubscribeUrlConfig.All_DefaultVaule.ACCESSTOKEN_VALUE);
        map.put(SubscribeUrlConfig.Params.CHANNEL, SubscribeUrlConfig.All_DefaultVaule.CHANNEL_VALUE);
        map.put(SubscribeUrlConfig.Params.VER, SubscribeUrlConfig.All_DefaultVaule.VER_VALUE);
        map.put(SubscribeUrlConfig.Params.RES, SubscribeUrlConfig.All_DefaultVaule.RES_VALUE);
        presenter.getStarFromNet(flag, map, this);
    }
    /**
     * 初始化搜索后的数据present 以及传参
     */
    private void initSrearchPresenter() {
        presenter = new StarListPresenter(StarListActivity.this);
        Map<String, String> map = new HashMap<>();
        map.put(SubscribeUrlConfig.Params.KEYWORD, mEditText.getText().toString());
        map.put(SubscribeUrlConfig.Params.PAGE, page + "");
        map.put(SubscribeUrlConfig.Params.SIZE, 20 + "");
        map.put(SubscribeUrlConfig.Params.TS, SubscribeUrlConfig.All_DefaultVaule.TS_VALUE);
        map.put(SubscribeUrlConfig.Params.TYPE, 1 + "");
        map.put(SubscribeUrlConfig.Params.MAC, SubscribeUrlConfig.All_DefaultVaule.MAC_VALUE);
        map.put(SubscribeUrlConfig.Params.UID, SubscribeUrlConfig.All_DefaultVaule.UID_VALUE);
        map.put(SubscribeUrlConfig.Params.PF, SubscribeUrlConfig.All_DefaultVaule.PF_VALUE);
        map.put(SubscribeUrlConfig.Params.NET, SubscribeUrlConfig.All_DefaultVaule.NET_VALUE);
        map.put(SubscribeUrlConfig.Params.ACCESSTOKEN, SubscribeUrlConfig.All_DefaultVaule.ACCESSTOKEN_VALUE);
        map.put(SubscribeUrlConfig.Params.CHANNEL, SubscribeUrlConfig.All_DefaultVaule.CHANNEL_VALUE);
        map.put(SubscribeUrlConfig.Params.VER, SubscribeUrlConfig.All_DefaultVaule.VER_VALUE);
        map.put(SubscribeUrlConfig.Params.RES, SubscribeUrlConfig.All_DefaultVaule.RES_VALUE);
        presenter.getStarFromNet(flag, map, StarListActivity.this);
    }

    @Override
    public void sendToAdapter(StarListAdapter adapter) {

    }

    /**
     * 获取list集合
     * @param list
     */
    @Override
    public void onLoadStarListSuccess(final List<ListBean> list) {
        //分页
        totalList.addAll(list);
        adapter.setList(totalList);
        adapter.setFlag(flag);
        if (page == 1) {
            mRecyclerView.setAdapter(adapter);
        } else {
            adapter.notifyDataSetChanged();
        }
        adapter.setListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                //跳转到明星内容页
                Intent intent = new Intent(StarListActivity.this, StarItemActivity.class);
                intent.putExtra("sid", list.get(position).getId() + "");
                startActivity(intent);
            }

            @Override
            public void onItemLongClick(View view, int position) {

            }
        });
    }

    /**
     * 获取搜索后的list集合
     * @param list
     */
    @Override
    public void onLoadSearchStarSuccess(final List<ListBean> list) {
        adapter.setList(list);
        adapter.setFlag(flag);
        if (page == 1) {
            if (list != null) {
                adapter.notifyDataSetChanged();
                mRecyclerView.setAdapter(adapter);
            }else{
                Toast.makeText(StarListActivity.this, "没有更多数据了", Toast.LENGTH_SHORT).show();
            }
        } else {
            if (list != null)
            adapter.notifyDataSetChanged();


        }
        adapter.setListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                //跳转到明星内容页
                Intent intent = new Intent(StarListActivity.this, StarItemActivity.class);
                intent.putExtra("sid", list.get(position).getId() + "");
                startActivity(intent);
            }

            @Override
            public void onItemLongClick(View view, int position) {

            }
        });
    }

    @Override
    public void onLoadStarListFail(String msg) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.starlist_back:
                //返回
                finish();
                break;
            case R.id.tv_starlist_search:
                //搜索
                if (mEditText != null) {
                    flag = true;
                    initSrearchPresenter();
                    adapter.notifyDataSetChanged();
                    InputMethodManager imm = (InputMethodManager) getSystemService(this.INPUT_METHOD_SERVICE);
                    // 隐藏软键盘
                    imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0);
                } else {
                    flag=false;
                    initPresenter();
                    adapter.notifyDataSetChanged();
                }
                break;
        }
    }
}
